package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.service.CarServiceZYH;
@Controller
@Scope(value="prototype")
public class BuyCarActionZYH extends ActionSupport implements ServletResponseAware,ServletRequestAware{
	
	private HttpServletRequest req;
	private HttpServletResponse resp;
	
	@Resource
	private CarServiceZYH carServiceZyh;
	
	public void buyCar(){
		try {
			req.setCharacterEncoding("utf8");
			resp.setCharacterEncoding("utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String carName = req.getParameter("carName");
		String carType = req.getParameter("carType");
		String carNumber = req.getParameter("carNumber");
		String dpName = req.getParameter("dpName");
		int dpId = carServiceZyh.findDpIdByDpName(dpName);
		int carMaxWeight = 0;
		int carid = carServiceZyh.findCarNumber(carNumber);
		PrintWriter pw;
		try {
			pw = resp.getWriter();
			if(carid>0){
				pw.write("3");
			}else{
				
			if("重型卡车".equals(carType)){
				pw.write("0");
				carMaxWeight = 10;
				carServiceZyh.insertCar(carName, carType,carNumber, carMaxWeight, "空闲",dpId);
			}else if("中型卡车".equals(carType)){
				pw.write("1");
				carMaxWeight = 6;
				carServiceZyh.insertCar(carName, carType, carNumber,carMaxWeight, "空闲",dpId);
			}else if("小型卡车".equals(carType)){
				pw.write("2");
				carMaxWeight = 2;
				carServiceZyh.insertCar(carName, carType, carNumber,carMaxWeight, "空闲",dpId);
			}
			
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.req = arg0;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.resp = arg0;
	}

}
